<?php

class LightboxgalleryConfiguration extends Configuration {
	protected $AppName = "lightboxgallery";
	protected $IncludeFiles = array("lbg.album.class.php", "lbg.image.class.php");
	public $UseApps = array(array("name" => "mysql"), array("name" => "smarty"), array("name" => "wideimage"));
	
	function registerMethods(AdminController $admin){
		$admin->register($this->AppName, "album-add", "addAlbumInterface", $this->Language->get("add_album"));		// DONE
		$admin->register($this->AppName, "album-list", "albumListInterface", $this->Language->get("album_list"));	// DONE
		$admin->register($this->AppName, "image-add", "addImageInterface", $this->Language->get("add_image"));		// DONE
		
		$admin->register($this->AppName, "album-delete", "deleteAlbumInterface", null);								// DONE
		$admin->register($this->AppName, "album-edit", "editAlbumInterface", null);									// DONE
		$admin->register($this->AppName, "image-delete", "deleteImageInterface", null);								
		$admin->register($this->AppName, "image-edit", "editImageInterface", null);
		$admin->register($this->AppName, "image-list", "imageListInterface", null);									// DONE
		$admin->register($this->AppName, "image-add-multiple", "addMultipleImagesInterface", null);
	}
	
	function addAlbumInterface(){
		if(!isset($_POST["addAlbum"])){
			$this->ViewFile = "config_add_album.tpl";
			return true;	
		}
		
		// Prepare album data
		$NewAlbumName = preg_replace("#[^A-Za-z0-9]#", "", strtolower($_POST["album_name"]));
		$NewAlbumDisplayName = escape($_POST["album_name"], $this->UseApps["mysql"]);
		$NewAlbumComment = escape($_POST["album_comment"], $this->UseApps["mysql"]);
		
		if(strlen($NewAlbumName) <= 0){
			return $this->Language->get("invalid_album_name");
		}
		
		// Checks if album with that name already exists
		$ExistingAlbum = new GalleryAlbum($NewAlbumName, $this);
		
		if($ExistingAlbum->getID() != null){
			return $this->Language->get("album_exists");
		}
		
		// Create dirs and insert new entry into database
		$MySQL = $this->UseApps["mysql"];
		
		$CreateDir = dirname(__FILE__)."/../../content/lbgallery/".$NewAlbumName;
		
		if(mkdir($CreateDir)) {
			chmod($CreateDir, 0777);
			$MySQL->query("INSERT INTO {$MySQL->getPrefix()}lightbox_albums (album_name, album_display_name, album_comment) VALUES('$NewAlbumName', '$NewAlbumDisplayName', '$NewAlbumComment')");
			
			return $this->Language->get("album_created");
		}
		else {
			return $this->Language->get("album_not_created");
		}
	}
	
	function albumListInterface(){
		$MySQL = $this->UseApps["mysql"];
		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
		
		$SelectAlbums = $MySQL->query("SELECT album_name FROM {$MySQL->getPrefix()}lightbox_albums ORDER BY id DESC");
		
		if(mysql_num_rows($SelectAlbums) <= 0){
			return $this->Language->get("no_albums");
		}
		
		$GalleryAlbums = array();
			
		while ($album = mysql_fetch_assoc($SelectAlbums)) {
			$GalleryAlbums[] = new GalleryAlbum($album["album_name"], $this);
		}
		
		$SmartyC->addHeadTag("<link rel=\"stylesheet\" type=\"text/css\" href=\"{\$env.url}lib/lightboxgallery/views/lbgallery.css\" />");
		$SmartyC->addHeadTag("<script type=\"text/javascript\" src=\"{\$env.url}lib/lightboxgallery/lbgallery.js\"></script>");	
	
		$Smarty->assign("GalleryAlbums", $GalleryAlbums);
		$this->ViewFile = "config_list_albums.tpl";
		
		return true;
	}
	
	function editAlbumInterface() {
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();
		
		$URL_Action = $HTTP->get($URLPosition+3);
		
		if($URL_Action == false){
			return $this->Language->get("not_found");
		}

		$MySQL = $this->UseApps["mysql"];

		// if album edit button has been pressed update album data in database
		if(isset($_POST["editAlbum"]) && !empty($_POST["album_name"]) && !empty($_POST["album_comment"])){
			// Prepare album data
			$EditAlbumDisplayName = escape($_POST["album_name"], $MySQL);
			$EditAlbumComment = escape($_POST["album_comment"], $MySQL);
			
			$UpdateSQL = $MySQL->generateUpdateQuery($MySQL->getPrefix()."lightbox_albums", array("album_display_name" => $EditAlbumDisplayName, "album_comment" => $EditAlbumComment), array("album_name" => $URL_Action));
			
			$MySQL->query($UpdateSQL);

			return $this->Language->get("album_edited");			
		}
		
		// if album edit button has not been pressed, display interface
		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
		
		$Album = new GalleryAlbum($URL_Action, $this);
		
		$Smarty->assign("Album", $Album);
		
		$this->ViewFile = "config_edit_album.tpl";
		
		return true;
	}

	function deleteAlbumInterface(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();
		
		$URL_Action = $HTTP->get($URLPosition+3);
		
		if($URL_Action == false){
			return $this->Language->get("not_found");
		}

		$MySQL = $this->UseApps["mysql"];

		$MySQL->query("DELETE FROM {$MySQL->getPrefix()}lightbox_albums WHERE album_name = '$URL_Action'");

		return $this->Language->get("album_deleted");	
	}

	function imageListInterface(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();
		
		$URL_Action = $HTTP->get($URLPosition+3);

		if(empty($URL_Action)){
			return $this->Language->get("not_found");
		}

		$MySQL = $this->UseApps["mysql"];
		
		$Album = new GalleryAlbum($URL_Action, $this);		

		if($Album->getID() == null){
			return $this->Language->get("invalid_album_name");
		}
		
		$ImageList = $MySQL->query("SELECT * FROM {$MySQL->getPrefix()}lightbox_images WHERE album_id = {$Album->getID()}");

		$AlbumImages = array();

		while($image = mysql_fetch_assoc($ImageList)){
			$AlbumImages[] = new GalleryImage($image["id"], $this);
		}

		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
		
		$SmartyC->addHeadTag("<link rel=\"stylesheet\" type=\"text/css\" href=\"{\$env.url}lib/lightboxgallery/views/lbgallery.css\" />");		
		$SmartyC->addHeadTag("<script type=\"text/javascript\" src=\"{\$env.url}lib/lightboxgallery/lbgallery.js\"></script>");	

		$Smarty->assign("AlbumImages", $AlbumImages);		

		$this->ViewFile = "config_image_list.tpl";
		
		return true;
	}
	
	function addImageInterface(){
		$MySQL = $this->UseApps["mysql"];
		
		if(isset($_POST["addImage"])){
			if($this->addImage($_POST, $_FILES["upload_image"]) == true){
				return $this->Language->get("image_added");		
			}
		}
		// Select albums, assign them to Smarty and set template file
		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
	
		$ListOfAlbums = $MySQL->query("SELECT album_name FROM {$MySQL->getPrefix()}lightbox_albums ORDER BY album_name ASC");
	
		$Albums = array();
	
		while ($album = mysql_fetch_assoc($ListOfAlbums)) {
			$Albums[] = new GalleryAlbum($album["album_name"], $this);
		}
	
		$Smarty->assign("AlbumList", $Albums);
	
		$this->ViewFile = "config_add_image.tpl";
	
		return true;
	}
	
	function uploadImage($file_information, $album_name){
		$AlbumDirPath = dirname(__FILE__)."/../../content/lbgallery/".$album_name."/";
	
		$NewFileName = preg_replace("#[^a-z0-9\.]#", "", strtolower($file_information["name"]));
	
		if(move_uploaded_file($file_information["tmp_name"], $AlbumDirPath.$NewFileName)){
			// Generate thumbnail image
			$ExplodeName = explode(".", $NewFileName);
			$Thumbnail = "";
			$NumberTokens = count($ExplodeName);
			for ($i = 0; $i <= $NumberTokens-2;$i++){
				$Thumbnail .= $ExplodeName[$i];
			}
			$Thumbnail .= "_thumb.".$ExplodeName[$NumberTokens-1];
			
			wiImage::load($AlbumDirPath.$NewFileName)->resize(100, 70)->saveToFile($AlbumDirPath.$Thumbnail);
			
			return array($NewFileName, $Thumbnail);
		}
	
		return false;
	}

	function addImage($post_data, $files_data){
		$MySQL = $this->UseApps["mysql"];
		
		if(isset($post_data["addImage"]) && !empty($post_data["image_display_name"]) && !empty($files_data) && !empty($post_data["album_name"])){
		
			// Check if album exists
			$Album = new GalleryAlbum(escape($post_data["album_name"], $MySQL), $this);
			
			if($Album->getID() == null){
				return $this->Language->get("album doesn't exist"); // POPRAVI
			}
		
			// Prepare image data and upload image
			$ImageDisplayName = escape($post_data["image_display_name"], $MySQL);
			$ImageComment = escape($post_data["image_comment"], $MySQL);
			$UploadImage = $this->uploadImage($files_data, $post_data["album_name"]);
			$ImageName = $UploadImage[0];
			$ThumbName = $UploadImage[1];
			$AlbumID = $Album->getID();
			
			// Insert new image into database
			$MySQL->query("INSERT INTO {$MySQL->getPrefix()}lightbox_images (image_name, image_file_name, image_thumbnail, image_comment, album_id) VALUES('$ImageDisplayName', '$ImageName', '$ThumbName', '$ImageComment', $AlbumID)");
		
			return true;
		}
		return false;
	}
	
	function editImageInterface(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();
	
		// Checks if image exists
		$ImageID = $HTTP->get($URLPosition+3);
	
		if(!is_numeric($ImageID)){
			return $this->Language->get("invalid_image_id");
		}
	
		$MySQL = $this->UseApps["mysql"];
	
		if(isset($_POST["editImage"]) && !empty($_POST["image_display_name"]) && !empty($_POST["album_name"])){
			// Generate album and check if album exists
			$Album = new GalleryAlbum(escape($_POST["album_name"], $MySQL), $this);
		
			if($Album->getID() == null){
				return $this->Language->get("invalid_album_name");
			}
		
			// Prepare edited image data
			$EditImageDisplayName = escape($_POST["image_display_name"], $MySQL);
			$EditImageComment = escape($_POST["image_comment"], $MySQL);
			$EditAlbumID = $Album->getID();
			
			// Generate update query and update image data in database
			$UpdateSQL = $MySQL->generateUpdateQuery($MySQL->getPrefix()."lightbox_images", array("image_name" => $EditImageDisplayName, "image_comment" => $EditImageComment, "album_id" => $EditAlbumID), array("id" => $ImageID));
		
			$MySQL->query($UpdateSQL);
		
			return $this->Language->get("image_updated");
		}
	
		// Select albums, assign them to Smarty and set template file
		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
	
		$ListOfAlbums = $MySQL->query("SELECT album_name FROM {$MySQL->getPrefix()}lightbox_albums ORDER BY album_name ASC");
	
		$Albums = array();
	
		while ($album = mysql_fetch_assoc($ListOfAlbums)) {
			$Albums[] = new GalleryAlbum($album["album_name"], $this);
		}
		
		$GalleryImage = new GalleryImage($ImageID, $this);
		
		$Smarty->assign("AlbumList", $Albums);
		$Smarty->assign("GalleryImage", $GalleryImage);
		
		$this->ViewFile = "config_edit_image.tpl";
	
		return true;
	}
	
	function deleteImageInterface(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();
		
		$ImageID = $HTTP->get($URLPosition+3);
		
		if(!is_numeric($ImageID)){
			return $this->Language->get("invalid_image_id");
		}
		
		$MySQL = $this->UseApps["mysql"];
		
		$MySQL->query("DELETE FROM {$MySQL->getPrefix()}lightbox_images WHERE id = $ImageID");
		
		return $this->Language->get("image_deleted");
	}
	
	
	function addMultipleImagesInterface(){
		$MySQL = $this->UseApps["mysql"];
	
		if(isset($_POST["addImage"])){
			
			for($i = 0; $i <= count($_FILES["upload_image"]); $i++){
				$image_attrs = array();
				
				if(!isset($_FILES["upload_image"]["name"][$i])){
					break;
				}
				
				$image_attrs["name"] = $_FILES["upload_image"]["name"][$i];
				$image_attrs["type"] = $_FILES["upload_image"]["type"][$i];
				$image_attrs["tmp_name"] = $_FILES["upload_image"]["tmp_name"][$i];
				$image_attrs["error"] = $_FILES["upload_image"]["error"][$i];
				$image_attrs["size"] = $_FILES["upload_image"]["size"][$i];

				if($this->addImage($_POST, $image_attrs) != true){
					return $this->Language->get("error_uploading")." ".$image_attrs["name"];
				}
			}
			
			return $this->Language->get("images_added");
		}
		
		// Select albums, assign them to Smarty and set template file
		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
		
		$SmartyC->addHeadTag("<script src=\"{\$env.url}other/javascript/jquery/fileUpload/jquery.MultiFile.js\" type=\"text/javascript\" language=\"javascript\"></script>");
	
		$ListOfAlbums = $MySQL->query("SELECT album_name FROM {$MySQL->getPrefix()}lightbox_albums ORDER BY album_name ASC");
	
		$Albums = array();
	
		while ($album = mysql_fetch_assoc($ListOfAlbums)) {
			$Albums[] = new GalleryAlbum($album["album_name"], $this);
		}
	
		$Smarty->assign("AlbumList", $Albums);
	
		$this->ViewFile = "config_add_images.tpl";
		return true;
	}
}

?>